package xyz.apiote.bimba.czwek.settings.feeds

import android.annotation.SuppressLint
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.edit
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.materialswitch.MaterialSwitch
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.api.FeedInfoV1
import xyz.apiote.bimba.czwek.api.Server
import java.net.URLEncoder


class BimbaFeedInfoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
	val root: View = itemView.findViewById(R.id.feed)
	val switch: MaterialSwitch = itemView.findViewById(R.id.feed_switch)
	val name: TextView = itemView.findViewById(R.id.feed_name)

	companion object {
		fun bind(
			feed: FeedInfoV1,
			context: Context,
			holder: BimbaFeedInfoViewHolder?,
			onClickListener: (FeedInfoV1) -> Unit
		) {
			val shp = context.getSharedPreferences(URLEncoder.encode(Server.get(context).apiPath, "utf-8"), MODE_PRIVATE)
			holder?.root?.setOnClickListener {
				onClickListener(feed)
			}
			holder?.name?.text = feed.name
			holder?.switch?.apply {
				isChecked = shp.getBoolean(feed.id, false)
				setOnCheckedChangeListener { _, isChecked ->
					shp.edit(true) {
						putBoolean(feed.id, isChecked)
					}
				}
			}
		}
	}
}

class BimbaFeedInfoAdapter(
	private val inflater: LayoutInflater,
	private var feeds: List<FeedInfoV1>,
	private val context: Context,
	private val onClickListener: ((FeedInfoV1) -> Unit)
) :
	RecyclerView.Adapter<BimbaFeedInfoViewHolder>() {
	override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BimbaFeedInfoViewHolder {
		val rowView = inflater.inflate(R.layout.feedinfo, parent, false)
		return BimbaFeedInfoViewHolder(rowView)
	}

	override fun onBindViewHolder(holder: BimbaFeedInfoViewHolder, position: Int) {
		BimbaFeedInfoViewHolder.bind(feeds[position], context, holder, onClickListener)
	}

	override fun getItemCount(): Int = feeds.size

	@SuppressLint("NotifyDataSetChanged") // todo [3.2] DiffUtil
	fun update(items: List<FeedInfoV1>) {
		feeds = items.sortedBy { it.name }
		notifyDataSetChanged()
	}
}

class FeedBottomSheet(private var feed: FeedInfoV1) : BottomSheetDialogFragment() {
	companion object {
		const val TAG = "DepartureBottomSheet"
	}

	override fun onCreateView(
		inflater: LayoutInflater,
		container: ViewGroup?,
		savedInstanceState: Bundle?
	): View {
		val content = inflater.inflate(R.layout.feed_bottom_sheet, container, false)
		content.findViewById<TextView>(R.id.title).text = feed.name
		content.findViewById<TextView>(R.id.description).text = feed.description
		content.findViewById<TextView>(R.id.attribution).text = feed.attribution
		content.findViewById<TextView>(R.id.update_time).text =
			getString(R.string.last_update, feed.formatDate())
		return content
	}
}